package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
/**
 * 攻略文章资源控制器
 */
@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyDetailService strategyDetailService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model){
        qo.setOrderBy("sd.sequence");
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.selectAll(null));
        return "strategyDetail/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(MultipartFile file, StrategyDetail strategyDetail){
        JsonResult jsonResult = new JsonResult();
        if (file!=null && file.getSize()>0){
            String coverUrl = UploadUtil.uploadQiniu(file);
            strategyDetail.setCoverUrl(coverUrl);
        }
        try {
            strategyDetailService.saveOrUpdate(strategyDetail);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作失败!");
        }
        return jsonResult;
    }

    @RequestMapping("selectContentById")
    @ResponseBody
    public StrategyContent selectContentById(Long id){
        return strategyDetailService.selectContentById(id);
    }

}
